package com.crm.controller.adminController;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.crm.dao.UserDAO;
import com.crm.dao.UserRolesDAO;
import com.crm.entity.Business;
import com.crm.entity.User;
import com.crm.entity.UserRoles;

@Controller
@RequestMapping("/users")
public class UsersController {
	protected final Log logger = LogFactory.getLog(getClass());
	UserDAO udao=new UserDAO();
	UserRolesDAO rolesdao=new UserRolesDAO();
	@RequestMapping("/show.do")
	public ModelAndView show(ModelAndView model,HttpSession session) throws Exception{
		Business bus=(Business)session.getAttribute("business");
		int busid=bus.getBusinessId();
		List<UserRoles> lur=rolesdao.getRoles();
		logger.info("roles Are"+lur);
		User u=new User();
		List<User> lus=udao.findAll(busid);
		return model=new ModelAndView("/jsp/Users.jsp","command",u)
		.addObject("menuName","admin")
		.addObject("submenu","admin")
		.addObject("roles",lur)
		.addObject("usersList",lus);
	}
	@RequestMapping("/save.do")
	public ModelAndView save(ModelAndView model,BindingResult result,User user,HttpServletRequest request,HttpServletResponse response,HttpSession session) throws Exception{
		Business bus=(Business)session.getAttribute("business");
		user.setBusiness(bus);
		String msg=null;
		try{
			udao.insert(user);
			msg="Successfully Created User";
		}catch(Exception ex){
			ex.printStackTrace();
			msg="Failed To Create User.Please Try Again";
		}
		User u=new User();
		return model=new ModelAndView("/jsp/Users.jsp","command",u)
		.addObject("menuName","admin")
		.addObject("submenu","admin")
		.addObject("msg",msg);
	}
	@RequestMapping("/edit.do")
	public ModelAndView edit(ModelAndView model,BindingResult result,User user,HttpServletRequest request,HttpServletResponse response,HttpSession session) throws Exception{
		Business bus=(Business)session.getAttribute("business");
		int busid=bus.getBusinessId();
		List<User> u=new ArrayList<User>();
		u=udao.findAll(busid);
		return model=new ModelAndView("/jsp/Users.jsp","command",u)
		.addObject("menuName","admin")
		.addObject("submenu","admin")
		.addObject("msg","Successfully Created User");
	}
	@RequestMapping("/getAllUsers.do")
	public @ResponseBody String getAllUsers(ModelAndView model,BindingResult result,User user,HttpServletRequest request,HttpServletResponse response,HttpSession session) throws Exception{
		Business bus=(Business)session.getAttribute("business");
		int busid=bus.getBusinessId();
		List<User> userList=new ArrayList<User>();
		userList=udao.findAll(busid);
		StringBuilder sb=new StringBuilder();
		for(User u:userList){
			sb.append(u.getUserName()+"@"+u.getId()+",");
		}
		return sb.toString();
	}
	@RequestMapping("/editSave.do")
	public ModelAndView editSave(ModelAndView model,BindingResult result,User user,HttpServletRequest request,HttpServletResponse response,HttpSession session) throws Exception{
		Business bus=(Business)session.getAttribute("business");
		user.setBusiness(bus);
		udao.insert(user);
		User u=new User();
		return model=new ModelAndView("/jsp/Users.jsp","command",u)
		.addObject("menuName","admin")
		.addObject("submenu","admin")
		.addObject("msg","Successfully Updtaed User");
	}
	@RequestMapping("/delete.do")public ModelAndView delete(ModelAndView model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		Business bus=(Business)session.getAttribute("business");
		String sUserId=request.getParameter("userId");
		String msg=null;
		if(sUserId!=null){
			int userId=Integer.parseInt(sUserId);
			try {
				udao.deleteUser(userId,bus.getBusinessId());
				msg="Successfully Deleted User";
			} catch (HibernateException e) {
				e.printStackTrace();
				msg="Failed To Delete User";
			} catch (Exception e) {
				e.printStackTrace();
				msg="Failed To Delete User";
			}
		}
		return new ModelAndView("redirect:show.do","msg",msg);
	}
}
